import { ref } from "vue";
import { ForSelect, GetAccessRight } from "@/api/accessGroup";

const accessRight = ref<string[]>([]);
const forSelect = new ForSelect();
const getAccessRightApi = new GetAccessRight();

export default () => {
  const getAccessRight = () => {
    if (accessRight.value.length !== 0) return;
    let accessRightStr = sessionStorage.getItem("accessRight");
    if (accessRightStr) {
      accessRight.value = JSON.parse(accessRightStr);
    } else {
      // let meInfo = JSON.parse(sessionStorage.getItem('meInfo') || '{}')
      // if (meInfo) {
      //   forSelect.setQuery({
      //     userId: meInfo.userId
      //   }).fetch().then(res => {
      //     if (res.data.length > 0) {
      //       let item = res.data[0]
      //       getAccessRightApi.setQuery({
      //         accessGroupId: item.accessGroupId
      //       }).fetch().then(res => {
      //         accessRight.value = (<any>res.data.accessRights)?.split(',')
      //         sessionStorage.setItem('accessRight', JSON.stringify((<any>res.data.accessRights)?.split(',')))
      //       })
      //     }
      //   })
      // }
    }
  };

  const hasAccess = (accessCode: string) => {
    getAccessRight();
    let index = accessRight.value.findIndex((i) => i === accessCode);
    return index !== -1;
  };

  return {
    getAccessRight,
    accessRight,
    hasAccess,
  };
};
